//
//  LLT_NewsTableViewCell_LXH.m
//  CoderLLT
//
//  Created by lanou3g on 15/12/10.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "LLT_NewsTableViewCell_LXH.h"
#import "LLT_NewsModel.h"
#import "UIImageView+WebCache.h"


@interface LLT_NewsTableViewCell_LXH()


@property (weak, nonatomic) IBOutlet UIImageView *LLT_NewsImageView;

@property (weak, nonatomic) IBOutlet UILabel *LLT_NewsTitleLabel;

@property (weak, nonatomic) IBOutlet UILabel *LLT_NewsContentLabel;

@property (weak, nonatomic) IBOutlet UILabel *LLT_NewsTimeLabel;

@end

@implementation LLT_NewsTableViewCell_LXH

/**
 * 销毁cell
 */
-(void)dealloc{
    
    self.LLT_NewsImageView = nil;
    
    self.LLT_NewsTitleLabel = nil;
    
    self.LLT_NewsContentLabel = nil;
    
    self.LLT_NewsTimeLabel = nil;
    
}


- (void)awakeFromNib {
    // Initialization code    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


/**
 * 设置cell的newsModel
 */
-(void)setNewsModel:(LLT_NewsModel *)newsModel{
    
    if (_newsModel != newsModel) {
        
        _newsModel = nil;
        
        _newsModel = newsModel;
        
        [self setNewsWithModel];
    }
    
}

/**
 * 设置cell上每个属性的值
 */
-(void)setNewsWithModel{

    self.LLT_NewsTitleLabel.text = [NSString stringWithFormat:@"%@",self.newsModel.title];
    
    self.LLT_NewsContentLabel.text = [NSString stringWithFormat:@"%@",self.newsModel.descriptionNews];
    
    self.LLT_NewsTimeLabel.text = [self getNewsDateLabelTextByString:self.newsModel.date];
    
    if (self.newsModel.imgs.count > 0) {
        
        [self.LLT_NewsImageView sd_setImageWithURL:[NSURL URLWithString:self.newsModel.imgs[0]] placeholderImage:[UIImage imageNamed:@"placeHolder"]];
    }else{
        
        self.LLT_NewsImageView.image = [UIImage imageNamed:@"placeHolder"];
        
    }

}

/**
 * 计算时间的方法
 */
-(NSString *)getNewsDateLabelTextByString:(NSString *)string{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    if (string.length == 19) {
        
        formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        
    }else{
        
        formatter.dateFormat = @"yyyy-MM-dd HH:mm";
        
    }
    
    NSDate *date = [formatter dateFromString:string];
    
    
    NSTimeInterval interval = [date timeIntervalSinceNow];
    
    NSMutableString *s = [NSMutableString string];
    
    if (interval > -1 * 60 *60) {
        NSInteger count = interval / 60;
        s = [NSMutableString stringWithFormat:@"🕘 %ld分钟前",-count];
    }else if(interval > - 24 * 60 * 60) {
        NSInteger count = interval / (60 * 60);
        s = [NSMutableString stringWithFormat:@"🕘 %ld小时前",-count];
    }else if (interval > - 30 * 24 * 60 * 60){
        NSInteger count = interval / (24 * 60 * 60);
        s = [NSMutableString stringWithFormat:@"🕘 %ld天前",-count];
    }else{
        NSInteger count = interval / (24 * 60 * 60);
        s = [NSMutableString stringWithFormat:@"🕘 %ld天前",-count];
    }
    return s;
}


@end
